package com.algorithm.thePrimaryAlgorithm;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class StringSulotionLC15 {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        Map<Character, Long> smap = new HashMap<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (smap.get(c) != null) {
                smap.put(c, smap.get(c) + 1);
            } else {
                smap.put(c, 1L);
            }
        }

        char[] chars1 = t.toCharArray();
        for (int i = 0; i < chars1.length; i++) {
            char c1 = chars1[i];
            if(smap.get(c1)!=null){
                smap.put(c1,smap.get(c1)-1);
            }
        }

        for (Map.Entry<Character, Long> entry: smap.entrySet()){
            if (entry.getValue()!=0){
                return false;
            }
        }
        return true;
    }

    @Test
    public void test(){
        String s = "anagram";
        String t = "nagaram";
        boolean anagram = isAnagram(s, t);
        log.info("over {}",anagram);
    }

    /**
     * 这个只能说明方法应的比较熟
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram1(String s, String t) {
        if (s.length()!=t.length()){
            return false;
        }

        char[] sa = s.toCharArray();
        char[] ta = t.toCharArray();
        Arrays.sort(sa);
        Arrays.sort(ta);
        return Arrays.equals(sa,ta);
    }

    @Test
    public void test1(){
        String s = "aa", t = "bb";
        boolean anagram = isAnagram(s, t);
        log.info("over {}",anagram);
    }
}
